package com.tanfuqi.sicent.server.controller

import com.tanfuqi.sicent.protocol.message.Member
import com.tanfuqi.sicent.protocol.message.Message
import com.tanfuqi.sicent.protocol.message.QueryBody
import com.tanfuqi.sicent.server.filter.sent
import io.swagger.annotations.ApiOperation
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono
import reactor.core.publisher.toMono

@RestController
class MemberController : BaseController() {

    @ApiOperation(value = "查询会员")
    @GetMapping("/shop/{shopId}/member/{value}/type/{type}")
    fun get(@PathVariable shopId: String, @PathVariable value: String, @PathVariable type: Int): Mono<Any> =
            session(shopId).sent(Message(0x8003, body = QueryBody(value, type))).map { it.body }

    @ApiOperation(value = "添加会员")
    @PostMapping("/shop/{shopId}/member")
    @ResponseStatus(HttpStatus.CREATED)
    fun post(@PathVariable shopId: String, @RequestBody member: Member): Mono<Any> =
            session(shopId).sent(Message(0x8041, body = member)).map { it.body }

    @ApiOperation(value = "修改会员")
    @PutMapping("/shop/{shopId}/member/{cardId}")
    fun put(@PathVariable shopId: String, @PathVariable cardId: String, @RequestBody member: Member): Mono<Any> =
            member.toMono().doOnSuccess { member.cardId = cardId }.flatMap { it ->
                session(shopId).sent(Message(0x8043, body = it)).map { it.body }
            }



//            {
//        fee.cardId = cardId
//        return service!!.mode(fee)
//    }

//
//    @Throws(ApiException::class)
//    fun refund(recharge: Recharge): SicentMessageResponse {
//        recharge.setPrincipal(recharge.getAmount())
//        recharge.setAmount(null)
//        return shopService.sentMessage(0x804C, recharge)
//    }
//
//    @Throws(ApiException::class)
//    fun mode(fee: Fee): SicentMessageResponse {
//        return shopService.sentMessage(0x8048, fee)
//    }
//
//    @Throws(ApiException::class)
//    fun rechargeTemp(recharge: TempRecharge): SicentMessageResponse {
//        return shopService.sentMessage(0x8067, recharge)
//    }
//
//    @Throws(ApiException::class)
//    fun refundTemp(recharge: TempRecharge): SicentMessageResponse {
//        return shopService.sentMessage(0x8068, recharge)
//    }
//
//    @Throws(ApiException::class)
//    fun active(activeInfo: ActiveInfo): SicentMessageResponse {
//        return shopService.sentMessage(0x8044, activeInfo)
//    }
//
//    @Throws(ApiException::class)
//    fun activeTemp(tempMember: TempMember): SicentMessageResponse {
//        return shopService.sentMessage(0x8061, tempMember)
//    }
//
//    @Throws(ApiException::class)
//    fun inactive(activeInfo: ActiveInfo): SicentMessageResponse {
//
//        return shopService.sentMessage(0x8045, activeInfo)
//    }
//
//    @Throws(ApiException::class)
//    fun inactiveTemp(tempMember: TempMember): SicentMessageResponse {
//        return shopService.sentMessage(0x8062, tempMember)
//    }
//
//    @Throws(ApiException::class)
//    fun onlineTemp(online: Online): SicentMessageResponse {
//        return shopService.sentMessage(0x8063, online)
//    }
//
//    @Throws(ApiException::class)
//    fun offlineTemp(online: Online): SicentMessageResponse {
//        return shopService.sentMessage(0x8064, online)
//    }
//
//    @Throws(ApiException::class)
//    fun swich(swich: Swich): SicentMessageResponse {
//        return shopService.sentMessage(if (swich.getCardId().startsWith("-")) 0x8066 else 0x8049, swich)
//    }
//
////    public SicentMessageResponse online() throws ApiException {
////        SicentQuery query = new SicentQuery(0, "");
////        return shopService.sentMessage(0x8005, query);
////    }
//
//    @Throws(ApiException::class)
//    fun online(sq: SicentQuery): SicentMessageResponse {
//        return shopService.sentMessage(0x8005, sq)
//    }
//
//    @Throws(ApiException::class)
//    fun online(online: Online): SicentMessageResponse {
//        return shopService.sentMessage(0x8046, online)
//    }
//
//    @Throws(ApiException::class)
//    fun offline(online: Online): SicentMessageResponse {
//        return shopService.sentMessage(0x8047, online)
//    }
//
//    @Throws(ApiException::class)
//    fun active(): SicentMessageResponse {
//        val query = SicentQuery(0, "")
//        return shopService.sentMessage(0x8004, query)
//    }
}
